package edu.gdin.ecommerce.action;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import edu.gdin.ecommerce.model.CardBatch;
import edu.gdin.ecommerce.model.CardIssuer;
import edu.gdin.ecommerce.model.Inventory;
import edu.gdin.ecommerce.model.User;
import edu.gdin.ecommerce.service.ICardBatchService;
import edu.gdin.ecommerce.service.ICardIssuerService;
import edu.gdin.ecommerce.service.IInventoryService;
import edu.gdin.ecommerce.service.exception.ServiceException;
import edu.gdin.ecommerce.util.Pager;

@Component("cardBatchAction")
@Scope("prototype")
public class CardBatchAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8958464386679233597L;
	
	private ICardBatchService cardBatchService;
	private ICardIssuerService cardIssuerService;
	private IInventoryService inventoryService;
	private CardBatch cardBatch;
	private List<CardIssuer> cardIssuers;
	private Pager pager;
	
	private String exportBatchNo;
	private String makeCardBatchNo;
	private String filePath;
	private BigDecimal faceValue; // 面值
	private String operTime1;
	private String operTime2;
	private Integer currentPage = 1;
	private String msg;
	

	public ICardBatchService getCardBatchService() {
		return cardBatchService;
	}
	@Resource
	public void setCardBatchService(ICardBatchService cardBatchService) {
		this.cardBatchService = cardBatchService;
	}
	public ICardIssuerService getCardIssuerService() {
		return cardIssuerService;
	}
	@Resource
	public void setCardIssuerService(ICardIssuerService cardIssuerService) {
		this.cardIssuerService = cardIssuerService;
	}
	public IInventoryService getInventoryService() {
		return inventoryService;
	}
	@Resource
	public void setInventoryService(IInventoryService inventoryService) {
		this.inventoryService = inventoryService;
	}
	public CardBatch getCardBatch() {
		return cardBatch;
	}
	public void setCardBatch(CardBatch cardBatch) {
		this.cardBatch = cardBatch;
	}
	public List<CardIssuer> getCardIssuers() {
		return cardIssuers;
	}
	public void setCardIssuers(List<CardIssuer> cardIssuers) {
		this.cardIssuers = cardIssuers;
	}
	public Pager getPager() {
		return pager;
	}
	public void setPager(Pager pager) {
		this.pager = pager;
	}
	public String getExportBatchNo() {
		return exportBatchNo;
	}
	public void setExportBatchNo(String exportBatchNo) {
		this.exportBatchNo = exportBatchNo;
	}
	public String getMakeCardBatchNo() {
		return makeCardBatchNo;
	}
	public void setMakeCardBatchNo(String makeCardBatchNo) {
		this.makeCardBatchNo = makeCardBatchNo;
	}
	public String getFilePath() {
		return filePath;
	}
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	public BigDecimal getFaceValue() {
		return faceValue;
	}
	public void setFaceValue(BigDecimal faceValue) {
		this.faceValue = faceValue;
	}
	public String getOperTime1() {
		return operTime1;
	}
	public void setOperTime1(String operTime1) {
		this.operTime1 = operTime1;
	}
	public String getOperTime2() {
		return operTime2;
	}
	public void setOperTime2(String operTime2) {
		this.operTime2 = operTime2;
	}
	public Integer getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	
	// ----------  methods  -----------
	
	
	
	
	public String preAddBatch() {
		this.cardIssuers = this.cardIssuerService.getAllIssuer();
		return "preAddBatch";
	}
	
	
	public String addBatch() {
		Map<String, Object> session = (Map<String, Object>) ActionContext.getContext().getSession();
		User user = (User) session.get("user");

		try {
			this.cardBatchService.makeCardByBatch(this.cardBatch, user, this.faceValue);
		} catch (ServiceException e) {
			this.addFieldError("error", e.getMessage());
			
			this.cardIssuers = this.cardIssuerService.getAllIssuer();
			return "preAddBatch";
		}
		
		return "addBatch";
	}
	
	
	public String preQueryBatch() {
		this.cardIssuers = this.cardIssuerService.getAllIssuer();
		return "preQueryBatch";
	}
	
	
	public String queryBatch() {
		this.cardIssuers = this.cardIssuerService.getAllIssuer();
		this.pager = this.cardBatchService.queryBatch(this.cardBatch, this.operTime1, this.operTime2, this.currentPage);
		if(this.pager.getList().size() == 0) {
			this.addFieldError("msg", "没找到符合条件的制卡批次！");
		}
		return "queryBatch";
	}
	
	
	public String preExport() {
		this.cardBatchService.exportStatus(this.exportBatchNo);
		this.queryBatch();
		return "preExport";
	}
	
	
	public String exportByBatchNo() {
		this.filePath = this.cardBatchService.exportByBatchNo(this.exportBatchNo);
		return "exportByBatchNo";
	}
	
	
	public String makeCardStatus() {
		this.cardBatchService.makeCardStatus(this.makeCardBatchNo);
		this.queryBatch();
		return "makeCardStatus";
	}
	
}
